/*
 * Created on 19/Jan/2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package libjdc.dc.util;

/**
 * @author Daniel Botelho
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class XMLFilter {
	
	public static String encodeToXML(String message){
		String ret = message;
		ret = replaceAll(ret,'&',"&gt;");
		ret = replaceAll(ret,'\"',"&quot;");
		ret = replaceAll(ret,'<',"&lt;");
		ret = replaceAll(ret,'>',"&gt;");
		ret = replaceAll(ret,'\'',"&apos;");
		return ret;
	}
	
	public static String replaceAll(String message,char from,String to){
		String ret="";
		int len = message.length();
		for(int i=0; i < len ; i++ ){
			
			char c = message.charAt(i);
			
			if(c == from)
				ret += to;
			else if(c > 31 && c < 127)
				ret+= message.charAt(i);
		}
		return ret;
	}
	
	/**
     * Filter the specified message string for characters that are sensitive
     * in HTML.  This avoids potential attacks caused by including JavaScript
     * codes in the request URL that is often reported in error messages.
     *
     * @param message The message string to be filtered
     */
    public static String applyFilter(String message) {

        if (message == null)
            return (null);

        char content[] = new char[message.length()];
        message.getChars(0, message.length(), content, 0);
		StringBuffer result = new StringBuffer(content.length + 50);
		boolean in = false;
		for (int i = 0; i < content.length; i++) {
			if (content[i] > 31 && content[i] < 127)
				if (content[i] == '"') {
					in = !in;
					result.append(content[i]);
				} else if (in)
					switch (content[i]) {
						case '<' :
							result.append("&lt;");
							break;
						case '>' :
							result.append("&gt;");
							break;
						case '&' :
							result.append("&amp;");
							break;
						case '"' :
							result.append("&quot;");
							break;
						case '\'':
							result.append("&apos;");
							break;
						default :
							result.append(content[i]);
					}
				else
					result.append(content[i]);

		}
        return (result.toString());
    }
    
    public static String undoFilter(String message){
    	if (message == null)
            return (null);
    	
    	char content[] = new char[message.length()];
        message.getChars(0, message.length(), content, 0);
        
        String result ="";
        boolean in = false;
        for (int i = 0; i < content.length; i++) {
        	if(content[i]=='"')
        		in=!in;
        	
        	if(in && content[i]=='&'){
        		String cmd ="";
        		while(content[i]!=';')
        			cmd+=content[i++]+"";
        		cmd+=content[i];
        		if(cmd.equals("&lt;"))
        			result+='<';
        		else if(cmd.equals("&gt;"))
        			result+='>';
        		else if(cmd.equals("&amp;"))
        			result+='&';
        		else if(cmd.equals("&quot;"))
        			result+='\"';
        		else if(cmd.equals("&apos;"))
        			result+='\'';
        	}else
        		result+=content[i]+"";
        }
        return result; 
    }
}
